using System;
using System.Collections.Generic;
using System.Linq;
using System.Xml.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;


namespace Pokemon
{
    /// <summary>
    /// This is a game component that implements IUpdateable.
    /// </summary>
    public class Boom : PlayingItem
    {
        private int _damage;

        private int _health;

        private int _speed;

        private int _range;

        private int _delay;

        private int _indexTexture = 0;

        private bool _isDestroying = false;

        public Boom(Game game, string path, Blank owner, Vector2 position) : base(game, path, owner, position)
        {
            Initialize();

            _size = _textures[_indexTexture].Size;

            LoadContent();            
        }

        /// <summary>
        /// Allows the game component to perform any initialization it needs to before starting
        /// to run.  This is where it can query for any required services and load content.
        /// </summary>
        public override void Initialize()
        {
            XDocument doc = XDocument.Load(_path);
            XName name = XName.Get(@"Damages");

            if (doc.Document != null)
            {
                var definitions = doc.Document.Descendants(name);
                _damage = Int32.Parse((definitions.First()).Value);
                name = XName.Get("Healths");
                definitions = doc.Document.Descendants(name);
                _health = Int32.Parse((definitions.First()).Value);
                name = XName.Get("Speed");
                definitions = doc.Document.Descendants(name);
                _speed = Int32.Parse((definitions.First()).Value);
                name = XName.Get("Range");
                definitions = doc.Document.Descendants(name);
                _range = Int32.Parse((definitions.First()).Value);

                name = XName.Get("Delay");
                definitions = doc.Document.Descendants(name);
                _delay = Int32.Parse((definitions.First()).Value);

                name = XName.Get(@"Definition");
                definitions = doc.Document.Descendants(name);
                foreach (var definition in definitions)
                {
                    var xAttribute = definition.Attribute(@"SheetName");
                    if (xAttribute != null)
                    {
                        Texture2D tmp = Game.Content.Load<Texture2D>(xAttribute.Value);
                        Vector2 frameSize = Vector2.Zero;
                        Vector2 centerPoint = Vector2.Zero;

                        xAttribute = definition.Attribute(@"FrameWidth");
                        if (xAttribute != null)
                        {
                            var width = float.Parse(xAttribute.Value);
                            xAttribute = definition.Attribute(@"FrameHeight");
                            if (xAttribute != null)
                            {
                                var height = float.Parse(xAttribute.Value);
                                frameSize = new Vector2(width, height);                                
                            }
                        }

                        xAttribute = definition.Attribute(@"XCenterPoint");

                        var xCenterPoint = Int32.Parse(xAttribute.Value);
                        xAttribute = definition.Attribute(@"YCenterPoint");

                        var yCenterPoint = Int32.Parse(xAttribute.Value);

                        centerPoint = new Vector2(xCenterPoint, yCenterPoint);

                        _textures.Add(new Sprite(tmp, (int)(tmp.Width / frameSize.X), frameSize, centerPoint, false));
                    }
                }
            }            
            base.Initialize();
        }

        protected override void LoadContent()
        {
            _position.X = (_owner.Size.X - _size.X) / 2 + _owner.Position.X;
            _position.Y = _owner.Size.Y + _owner.Position.Y - _size.Y - 15;
        }

        /// <summary>
        /// Allows the game component to update itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        public override void Update(GameTime gameTime)
        {
            _delayTime += gameTime.ElapsedGameTime;

            _LifeTime -= gameTime.ElapsedGameTime;

            if (_LifeTime.TotalMilliseconds <= 0)
            {
                _state = ItemState.Destroy;
                _isDestroying = true;
                _indexTexture = 2;
            }

            if (_delayTime.TotalMilliseconds <= 200)
                return;

            _delayTime = TimeSpan.Zero;

            _textures[_indexTexture].Update();
        }

        public override void Draw(GameTime gameTime)
        {
            SpriteBatch sp = (SpriteBatch)Game.Services.GetService(typeof(SpriteBatch));

            _textures[_indexTexture].Draw(sp, _position, _color);

            if (_isDestroying && _textures[_indexTexture].FrameIndex == _textures[_indexTexture].NFrame - 1)
            {
                Vector2 centerPoint = _textures[_indexTexture].CenterPoint;
                if (_owner.IsContainZombie())
                    foreach (var drawableGameComponent in _owner.Components)
                    {
                        if (drawableGameComponent is Zombie)
                            (drawableGameComponent as Zombie).TakeDamage(_damage, Color.White);
                    }
                _owner.Remove(this);
            }    
        }

        //public delegate void DDestroy();

        //public event DDestroy Destroy;

        //public void OnDestroy()
        //{
        //    DDestroy handler = Destroy;
        //    if (handler != null) handler();
        //}

        //public void SwitchDestroyState()
        //{
            
        //}
    }
}
